﻿using HamcoWcs.Models.Common;
using HamcoWcs.Models.WcsLocations;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;

namespace System
{
    public static class StringExtension
    {
        static string SHELF_PATTERN = @"^\d{7}$";
        static string ZONE_PATTERN = @"^CJSTW[1-2]00Z[1-9]";
        static string SRM_PATTERN = @"^CJSTW[1-2]00-C[1-2]";

        /// <summary>
        /// 字符串转Int32
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int ToInt(this string s)
        {
            var a = int.TryParse(s, out int t);
            if (a) return t;
            return 0;
        }


        /// <summary>
        /// 判断字符串是否是储位名
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsShelf(this string s) => Regex.IsMatch(s, SHELF_PATTERN);
        /// <summary>
        /// 判断字符串是否是MES区域名
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsZone(this string s) => Regex.IsMatch(s, ZONE_PATTERN);
        /// <summary>
        /// 判断字符是否是堆垛机名
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsSrmName(this string s) => Regex.IsMatch(s, SRM_PATTERN);

        public static bool IsNullOrEmpty(this string s) => s == null || s.Length == 0;
        public static bool IsValid(this string s) => !s.IsNullOrEmpty();
        public static bool IsNumber(this string s) => Regex.IsMatch(s, @"^[0-9]*$");
        public static bool IsIdCard(this string s) => Regex.IsMatch(s, @"(^\d{18}$)|(^\d{15}$)");
        public static bool IsEmail(this string s) => Regex.IsMatch(s, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");

        public static string GetReturnCode(this string xml)
            => XmlHelper.GetXPathText(xml, "//RETURNCODE");
        public static LocationInfo ToLocationInfo(this string s)
        {
            //0100101
            var infos = s.Split('-');
            var loc = new LocationInfo();
            if (infos.Length == 2)
            {
                loc.Name = infos[0];
                loc.Depth = int.Parse(infos[1]);
            }
            else
            {
                loc.Name = infos[0];
                loc.Depth = 0;
            }
            return loc;
        }

        public static string FirstUpper(this string s)
        {
            if (s == null)
                return null;
            if (s.Length > 1)
                return char.ToUpper(s[0]) + s.Substring(1);
            return s.ToUpper();
        }
        public static string AddUnitid(this string s, string unitid)
        {
            if (s.IsNullOrEmpty())//isnullorempty
            {
                return unitid;
            }
            else
            {
                return s + '\\' + unitid;
            }
        }
        public static string SubUnitid(this string s)
        {
            if (!s.IsNullOrEmpty() && s.Contains("\\"))
            {
                return s.Split('\\')[0];
            }
            else
            {
                return "";
            }
        }

        public static string ReName(this string s)
        {
            return GlobalValue.AlarmDivceName.FirstOrDefault(x => s.Contains(x));
        }
    }

    public static class ByteExtension
    {
        public static byte[] Mid(this byte[] bytes, int start, int length = -1)
        {
            if (start < 0) throw new ArgumentOutOfRangeException("start", "起始位置错误");

            if (length < 0) length = bytes.Length - start;

            return bytes.Skip(start).Take(length).ToArray();
        }

        /// <summary>
        /// index 0-7
        /// </summary>
        /// <param name="data"></param>
        /// <param name="index"></param>
        /// <param name="flag"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static byte SetbitValue(byte data, int index, bool flag)
        {
            index++;
            if (index > 8 || index < 1)
                throw new ArgumentOutOfRangeException();
            int v = index < 2 ? index : (2 << (index - 2));
            return flag ? (byte)(data | v) : (byte)(data & ~v);
        }
    }
    public static class DateExtension
    {
        public static string GetCurrentTimestamp()
        => GetTimestamp(DateTime.Now);

        public static string GetTimestamp(DateTime date)
        => date.ToString("yyyyMMddHHmmssffffff");

        public static string GetDuration(this TimeSpan t)
        {
            if (t.Days > 0)
            {
                return t.ToString("dd\\.hh\\:mm\\:ss");
            }
            else
            {
                return t.ToString("hh\\:mm\\:ss");
            }
        }

        public static double GetSecond(this string s)
        {
            try
            {
                var a = TimeSpan.Parse(s);
                return a.TotalSeconds;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        private static CompareInfo _compare = CultureInfo.InvariantCulture.CompareInfo;
        public static bool ContainsIgnoreCase(this string s, string value)
        {
            //CompareInfo compare = CultureInfo.InvariantCulture.CompareInfo;
            return _compare.IndexOf(s, value, CompareOptions.OrdinalIgnoreCase) > -1;
        }
    }
}
